/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.gob.dirpen.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pe.gob.dirpen.entities.GenmaAnio;
import pe.gob.dirpen.service.AnioService;

/**
 *
 * @author gcarlos
 */
@Controller
public class AnioController {

    private static final Logger logger = Logger.getLogger(AnioController.class);
    
    @Autowired
    private AnioService anioService;

    @RequestMapping(value = "/listarAnio.htm")
    public String listarAnio(HttpServletRequest request,ModelMap model) {
        List<GenmaAnio> lstAnio = anioService.buscarAnios(new GenmaAnio());
        request.getSession().setAttribute("listaAnio", lstAnio);
        return "/sisinf/SeguridadMantenimiento/MantenerAnio/jsonListadoAnio";
    }

    @RequestMapping(value = "/registrarAnio.htm",method=RequestMethod.POST)
    public String registrarAnio(HttpServletRequest request,ModelMap model) {
        try {
            logger.debug("registrarAnio");
            GenmaAnio anio = new GenmaAnio();
            anio.setBanioActivo(Integer.parseInt(request.getParameter("cbestado")));
            anio.setCanioAnio(request.getParameter("txt_anio"));
            if( !Utiles.nullToBlank( request.getParameter("id_anio") ).equals("") )anio.setNanioIdentificador(Integer.parseInt(request.getParameter("id_anio")));
            anioService.registrarAnio(anio);
            model.put("exito","true");
            return "/general/exito";
        } catch (Exception e) {
            model.put("exito","false");
            return "/general/exito";
        }
    }
}
